package cc.vv.btpartyjob.partymanager.fragment

import android.content.Intent
import android.os.Message
import android.support.v4.widget.SwipeRefreshLayout
import android.util.Log
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.activity.WorkQingKuangDetailActivity
import cc.vv.btpartyjob.partymanager.adapter.WorkGoingAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.WorkGoingFragmentDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.SiZhiDuiWuBean
import cc.vv.btpartyjob.partymanager.moudel.resp.WorkGoingResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.mvp.fragment.BaseFragmentMVP
import com.chad.library.adapter.base.BaseQuickAdapter
import java.util.ArrayList

/**
 * 工作情况ragment
 * Created by Chen.qi on 2018/10/27.
 */
class GongZuoDuiWorkGoingFragment : BaseFragmentMVP<WorkGoingFragmentDelegate>(),
        BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {
    /**
     * 适配器
     */
    private var mAdapter: WorkGoingAdapter? = null
    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<SiZhiDuiWuBean>? = null

    private var whetherRefresh = false

    /**
     * 开始时间
     */
    private var startLongTime: Long = 0
    /**
     * 结束时间
     */
    private var endLongTime: Long = 0

    private var teamId: String? = null

    fun setTeamId(teamId: String?) {
        this.teamId = teamId
    }


    override fun initData() {
        if (mAdapter == null) {
            mDataList = ArrayList()
            mAdapter = WorkGoingAdapter(R.layout.item_sizhi_duiwu_workgoing)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
            viewDelegate?.setStartTime(null)
            viewDelegate?.setEndTime(null)
        }
    }

    /**
     * 更新数据
     */
    fun upData() {
        pageNo = 1
        viewDelegate?.setStartTime(null)
        viewDelegate?.setEndTime(null)
        startLongTime = System.currentTimeMillis()
        endLongTime = System.currentTimeMillis()
        PartyManagerServer.workGoingList(teamId, startLongTime, endLongTime, pageNo, pageSize, true)
    }

    override fun onVisibilityChanged(visible: Boolean) {
        super.onVisibilityChanged(visible)
        if (visible && !whetherRefresh) {
            startLongTime = System.currentTimeMillis()
            endLongTime = System.currentTimeMillis()
            pageNo = 1
            PartyManagerServer.workGoingList(teamId, startLongTime, endLongTime, pageNo, pageSize, true)
            whetherRefresh = true
        }
    }

    override fun bindEvenListener() {
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
        mAdapter?.disableLoadMoreIfNotFullPage()


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //开始时间
            ResourceChooseServer.chooseData(activity, handler, 10, 2)
        }, R.id.ll_fsdL_startTime)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //结束时间
            ResourceChooseServer.chooseData(activity, handler, 11, 2)
        }, R.id.ll_fsdL_endTime)
    }

    override fun getDelegateClass(): Class<WorkGoingFragmentDelegate> {
        return WorkGoingFragmentDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val intent = Intent()
        intent.putExtra("id", mDataList?.get(position)?.id)
        intent.setClass(activity, WorkQingKuangDetailActivity::class.java)
        startActivity(intent)
    }

    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            10 -> {
                //开始时间
                val dateString: String = msg.obj as String
                val start = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                if (start <= endLongTime!!) {
                    //请求数据
                } else {
                    LKToastUtil.showToastShort("开始时间不能大于结束时间，请重新选择")
                    return
                }
                startLongTime = start
                viewDelegate?.setStartTime(dateString)

                pageNo = 1
                PartyManagerServer.workGoingList(teamId, startLongTime, endLongTime, pageNo, pageSize, true)

            }

            11 -> {
                //结束时间
                val dateString: String = msg.obj as String
                val end = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                if (startLongTime <= end) {
                    //请求数据
                } else {
                    LKToastUtil.showToastShort("开始时间不能大于结束时间，请重新选择")
                    return
                }
                endLongTime = end
                viewDelegate?.setEndTime(dateString)

                pageNo = 1
                PartyManagerServer.workGoingList(teamId, startLongTime, endLongTime, pageNo, pageSize, true)
            }
        }

    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        if (whetherRefresh) {
            when (obj) {
                is WorkGoingResponseObj -> {
                    viewDelegate?.stopRefreshing()
                    if (null == obj.data) {
                        return
                    }
                    if (1 == pageNo) {
                        mDataList?.clear()
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        viewDelegate?.setAdapterData(mAdapter, mDataList)
                    } else {
                        if (mAdapter?.data?.size!! >= obj.data.total) {
                            //数据全部加载完毕
                            mAdapter?.loadMoreEnd(true)
                            mAdapter?.loadMoreEnd()
                        } else {
                            //成功获取更多数据
                            if (null != obj.data.records) {
                                mDataList!!.addAll(obj.data.records)
                            }
                            mAdapter?.setNewData(mDataList)
                            //加载数据
                            mAdapter?.disableLoadMoreIfNotFullPage()
                            mAdapter?.loadMoreComplete()
                        }
                    }
                }
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }

    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        PartyManagerServer.workGoingList(teamId, startLongTime, endLongTime, pageNo, pageSize, false)

    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        PartyManagerServer.workGoingList(teamId, startLongTime, endLongTime, pageNo, pageSize, false)

    }

}